<template>
  <section class="page upload-waper">

    <div class="upload-box" :class="{'reset': success}">
      <div class="up-content">
        <div class="upload-list" v-show="previewList.length" ref="uplist">
          <ul class="up-list-inner">
            <li class="up-li-item" v-for="(item, index) in previewList" :key="index">
              <div class="info">
                <p class="title">{{ item.file.name }}</p>
                <span class="size">
                  {{ $fn.turnSize(item.file.size) }} ·
                  {{ item.file.type }}
                  </span>
              </div>
              <Progress :s="item.s"></Progress>
              <i class="close-ctl" @click="removeItem(index)" title="移除">&times;</i>
            </li>
          </ul>
        </div>

        <div class="download-ctl-box" :class="{'min': previewList.length}">
          <div class="btn-download">
            <i class="el-icon-plus"></i>
          </div>
          <div class="down-info">
            <h4 class="title"> {{ previewList.length ? '上传更多文件': '上传文件' }}</h4>
            <p v-if="!previewList.length" class="desc">最大支持 500M 文件，会员可扩展到20G</p>
            <p v-else class="desc">
              已添加 {{previewList.length}} 个文件共
              {{$fn.turnSize(totalSize)}}B, 还可以上传 
              {{$fn.turnSize(laveSize)}}B
            </p>
          </div>
          <input class="input" type="file" @change="changeFile" multiple>
        </div>

        <div class="messge">
          <textarea class="textarea" placeholder="写个留言" v-model.trim="description"></textarea>
        </div>

      </div>
      <div class="up-footer">
        <el-button class="btn-content" type="primary" :disabled="disabled" @click="submitHandle">上传</el-button>
      </div>
    </div>

    <div class="upload-box right" :class="{'reset': !success}">
      <div class="up-content">
        <div class="see-head">
          <p class="info">总共 {{ fileList.length }} 个文件，共 {{$fn.turnSize(totalSize)}}</p>
          <p class="desc">{{ description || '没有留言' }}</p>
        </div>
      
        <div class="see-body">
          <div class="done-ico">Done</div>
          <p class="tip">复制分享链接</p>
          <el-input class="input" readonly :value="resLink"></el-input>
        </div>
        
      </div>
      <div class="up-footer">
        <a class="el-button btn-content el-button--primary" :href="resLink">预览</a>
      </div>
    </div>

  </section>
</template>

<script>
import axios from 'axios';
import Progress from './Progress';

export default {
  name: 'UploadBox',
  components: { Progress },
  data() {
    return {
      fileList: [],
      previewList: [],
      description: '',
      cacheLen: 0,

      resLink: '',
      success: false,
      disabled: false
    }
  },
  computed: {
    totalSize() {
      if (!this.previewList.length) {
        return 0
      }

      let size = 0;
      for (let i in this.previewList) {
        size+= this.previewList[i].file.size;
      }

      return size;
    },
    laveSize() {
      return (1024**3)*2 - this.totalSize;
    }
  },
  methods: {
    submitHandle() {
      if (!this.previewList.length) {
        return;
      }

      this.disabled = true;
      this.previewList.forEach(item => this.upLoadFile(item));
    },

    upLoadFile(item) {
      const file = item.file;
      const name = file.name;
      const formData = new FormData();

      formData.append('file', file);

      item.s = 'START';
      axios.post(`/upload`, formData).then(res => {
        item.s = 'STOP';
        this.fileList.push(res.data);

        this.isEnd();
      }).catch(err => {
        console.log(err);
        item.s = 'ERR';
        this.isEnd();
      });
    },

    isEnd() {
      this.cacheLen++;
      if (this.cacheLen < this.previewList.length) {
        return
      }
      
      axios.post('/save', {
        fileList: this.fileList,
        fileIds: this.fileList.map(v => v.id),
        description: this.description,
        filesize: this.totalSize,
      }).then(res => {
        this.resLink = location.origin + res.data.url;
        this.success = true;
      });
    },

    // 整理文件 
    changeFile(e) {
      [].forEach.call(e.currentTarget.files, file => {
        for (let i in this.previewList) {
          if ( this.previewList[i].file.name === file.name ) {
            this.previewList.splice(i, 1);
            break;
          }
        }
        
        this.previewList.push({file, s: ''});
        this.$nextTick(() => {
          const uplist = this.$refs.uplist;
          const H = uplist.clientHeight;
          const S = uplist.scrollHeight;
          uplist.scrollTo(0, S - H);
        })
      });
    },

    // 移除一个文件
    removeItem(index) {
      this.previewList.splice(index, 1);
    },

    shareView() {
      if (this.resLink) {
        window.location.href = this.resLink;
      }
    }
  }
}
</script>

<style lang="less">
@import '../style/_mixin.less';

@w: 380px;
@h: 470px;

.upload-waper {
  -webkit-perspective: 2000;
  perspective: 2000;
}
.upload-box {
  position: absolute;
  left: 50%;
  top: 50%;
  width: @w;
  height: @h;
  margin-left: -@w/2;
  margin-top: -@h/2 + 20px;

  border-radius: 4px;
  background: #fff;
  box-shadow: 0 8px 18px rgba(0, 0, 0, .1);

  transition: all .2s linear;

  &.reset {
    transform-origin: left;
    transform: rotateY(100deg);
    opacity: 0;
  }
  &.right.reset {
    transform-origin: right;
    transform: rotateY(-100deg);
    opacity: 0;
  }

  .up-content {
    height: @h - 80px;
  }

  .up-footer {
    padding-top: 20px;
    text-align: center;
    border-top: 1px solid #eee;
  }
}

.download-ctl-box {
  position: relative;
  padding: 30px 20px;
  border-bottom: 1px solid #eee;
  transition: all .2s ease-in-out;

  .btn-download {
    display: inline-block;
    width: 50px;
    height: 50px;
    background: @theme;
    vertical-align: -5px;
    border-radius: 50%;
    font-size: 22px;
    color: #fff;
    text-align: center;
    line-height: 50px;
    transition: all .2s ease-in-out;
  }
  .down-info {
    display: inline-block;
    vertical-align: middle;
    padding-left: 10px;

    .title {
      font-size: 20px;
      color: #020b15;
    }
    .desc {
      font-size: 14px;
      color: #7d7d7d;
    }
  }

  &.min {
    padding: 12px 20px;

    .btn-download {
      width: 32px;
      height: 32px;
      line-height: 32px;
      font-size: 20px;
    }
    .down-info {
      .title {
        font-size: 16px;
      }
      .desc {
        font-size: 12px;
      }
    }
  }

  .input {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    opacity: 0;
    cursor: pointer;
  }
}

.btn-content {
  width: 140px;
}

.messge {
  .textarea {
    padding: 14px;
    font-size: 14px;
    width: 100%;
    height: 115px;
    box-sizing: border-box;
    border: 0;
    resize: none;
    outline: none;
  }
}

.upload-list {
  padding: 8px 14px 0 14px;
  max-height: 200px;
  overflow: auto;
}
.up-li-item {
  position: relative;
  list-style: none;
  border-bottom: 1px dotted #ddd;
  padding: 6px 0;
  .info {
    display: inline-block;
    vertical-align: middle;
    font-size: 0;
  }
  .title {
    font-size: 12px;
    color: #555;
    line-height: 1.5;
  }
  .size {
    font-size: 12px;
    color: #888;
    line-height: 1.5;
  }
  .close-ctl {
    display: block;
    font-size: 15px;
    width: 17px;
    height: 17px;
    border-radius: 50%;
    font-style: normal;
    text-align: center;
    line-height: 15px;
    border: 1px solid #ddd;
    color: #ddd;
    margin-top: 8px;
    float: right;
    cursor: pointer;
    transition: all .2s ease;
    &:hover {
      border: 1px solid #409EFF;
      color: #409EFF;
    }
  }
}

.see-head {
  padding: 14px;
  border-bottom: 1px solid #eee;
  .info {
    font-size: 12px;
    color: #414141;
    line-height: 2;
  }
  .desc {
    font-size: 14px;
    color: #757575;
    height: 70px;
    overflow: auto;
  }
}

.see-body {
  padding: 14px;
  text-align: center;
  .done-ico {
    display: inline-block;
    width: 80px;
    height: 80px;
    line-height: 80px;
    font-weight: bold;
    font-size: 18px;
    border-radius: 50%;
    text-align: center;
    border: 3px solid #409EFF;
    margin: 26px;
  }
  .tip {
    font-size: 14px;
    color: #757575;
    margin-bottom: 10px;
  }
  .input {
    width: 200px;
  }
}
</style>
